package com.ymatou.iapi.productmanager.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.SetOnShelfUpdateStockNumCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.ProductStockInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_SetOnShelfUpdateStockNum {
	private static SetOnShelfUpdateStockNumCall setOnShelfUpdateStockNumCall;
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId =Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App-售罄商品上架");
		//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(sellerId);
	}
	
	@Before
	public void caseUp() {
		setOnShelfUpdateStockNumCall = new SetOnShelfUpdateStockNumCall();
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setOnShelfUpdateStockNumCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnShelfUpdateStockNum_001() {
		Logger.start(true,"App-售罄商品上架");
		try{
			
			int updateStock = 99;
			//1 add new product
			JSONObject productReqBean = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductId");
			

			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			YMTDateUtil.waitTime(1);
			
			//4 set stock = 0 
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			
			//5 SetOnShelfUpdateStockNum
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ActivityId", liveId);
			updateMap.put("ProductId", productId);
			updateMap.put("NewCatalogs[0]->PurchasedNum", updateStock);
			updateMap.put("UserId", sellerId);
			JSONObject requestBean = new SetOnShelfUpdateStockNumCall().callAndGetReturnData(updateMap); 
			Logger.verifyEquals( 200,requestBean.getInt("Code"), "响应报文-返回码");
	
			//check SQL & Mongo
			checkSetOnShelfUpdateStockNum(productId, updateStock);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnShelfUpdateStockNum_002() {
		Logger.start(true,"单规格、售罄商品上架并更新正常库存");
		try{
			int updateStock = 99;
			//1 add new product (单规格)
			JSONObject addProductforOneCatalog = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc2.json");
			Map<String, Object> updateBean = new HashMap<>();
			updateBean.put("UserID", sellerId);
			updateBean.put("LoginID", sellerName);
			JsonBeanHelper.updateJsonBean(addProductforOneCatalog, updateBean);
			
			JSONObject productReqBean = SellerProductCallService.addNewProduct(addProductforOneCatalog);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductId");
			

			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			YMTDateUtil.waitTime(1);
			
			//4 set stock = 0 
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			
			//5 SetOnShelfUpdateStockNum
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ActivityId", liveId);
			updateMap.put("ProductId", productId);
			updateMap.put("NewCatalogs[0]->PurchasedNum", updateStock);
			updateMap.put("UserId", sellerId);
			JSONObject requestBean = new SetOnShelfUpdateStockNumCall().callAndGetReturnData(updateMap); 
			Logger.verifyEquals( 200,requestBean.getInt("Code"), "响应报文-返回码");
			
	
			//check SQL & Mongo
			checkSetOnShelfUpdateStockNum(productId, updateStock);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
			
		}catch(Exception e){
			
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnShelfUpdateStockNum_003() {
		Logger.start(true,"多规格、售罄商品上架并更新正常库存");
		try{
			int updateStock = 99;
			//1 add new product (单规格)
			JSONObject addProductforOneCatalog = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc1.json");
			Map<String, Object> updateBean = new HashMap<>();
			updateBean.put("UserID", sellerId);
			updateBean.put("LoginID", sellerName);
			JsonBeanHelper.updateJsonBean(addProductforOneCatalog, updateBean);
			
			JSONObject productReqBean = SellerProductCallService.addNewProduct(addProductforOneCatalog);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductId");
			

			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			YMTDateUtil.waitTime(1);
			
			//4 set stock = 0 
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);

			
			//5 SetOnShelfUpdateStockNum
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ActivityId", liveId);
			updateMap.put("ProductId", productId);
			updateMap.put("NewCatalogs[0]->PurchasedNum", updateStock);
			updateMap.put("UserId", sellerId);
			JSONObject requestBean = new SetOnShelfUpdateStockNumCall().callAndGetReturnData(updateMap); 
			Logger.verifyEquals( 200,requestBean.getInt("Code"), "响应报文-返回码");
			
	
			//check SQL & Mongo
			checkSetOnShelfUpdateStockNum(productId, updateStock);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
			
		}catch(Exception e){
			
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnShelfUpdateStockNum_004() {
		Logger.start(true,"售罄商品上架并更新库存为0");
		try{
			
			
			int updateStock = 0;
			//1 add new product (单规格)
			JSONObject addProductforOneCatalog = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc1.json");
			Map<String, Object> updateBean = new HashMap<>();
			updateBean.put("UserID", sellerId);
			updateBean.put("LoginID", sellerName);
			JsonBeanHelper.updateJsonBean(addProductforOneCatalog, updateBean);
			
			JSONObject productReqBean = SellerProductCallService.addNewProduct(addProductforOneCatalog);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductId");
			

			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			YMTDateUtil.waitTime(1);
			
			//4 set stock = 0 
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);

			
			//5 SetOnShelfUpdateStockNum
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ActivityId", liveId);
			updateMap.put("ProductId", productId);
			updateMap.put("NewCatalogs[0]->PurchasedNum", updateStock);
			updateMap.put("UserId", sellerId);
			JSONObject requestBean = new SetOnShelfUpdateStockNumCall().callAndGetReturnData(updateMap); 
			Logger.verifyEquals( 200,requestBean.getInt("Code"), "响应报文-返回码");
			
	
			//check SQL & Mongo
			checkSetOnShelfUpdateStockNum(productId, updateStock);
			

			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
					NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
				}
		}catch(Exception e){
			
		}
	}	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnShelfUpdateStockNum_005() {
		Logger.start(true,"错误直播id, 售罄商品上架并更新库存为0");
		try{
			
			
			int updateStock = 0;
			//1 add new product (单规格)
			JSONObject addProductforOneCatalog = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc1.json");
			Map<String, Object> updateBean = new HashMap<>();
			updateBean.put("UserID", sellerId);
			updateBean.put("LoginID", sellerName);
			JsonBeanHelper.updateJsonBean(addProductforOneCatalog, updateBean);
			
			JSONObject productReqBean = SellerProductCallService.addNewProduct(addProductforOneCatalog);
			String productId =  productReqBean.getJSONObject("Data").getString("ProductId");
			

			
			
			YMTDateUtil.waitTime(1);
			
			//4 set stock = 0 
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);

			
			//5 SetOnShelfUpdateStockNum
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ActivityId", 12312);
			updateMap.put("ProductId", productId);
			updateMap.put("NewCatalogs[0]->PurchasedNum", updateStock);
			updateMap.put("UserId", sellerId);
			JSONObject requestBean = new SetOnShelfUpdateStockNumCall().callAndGetReturnData(updateMap); 
			Logger.verifyEquals( 200,requestBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals(true, requestBean.getJSONObject("Data").getJSONObject("Msg").getString("Msg").contains("没开始"), 
							"商品的相关扫货还没开始，不能进行商品上架");
			
	
		}catch(Exception e){
			
		}
	}	
	
	
	
	
	
	public static void checkSetOnShelfUpdateStockNum(String productId , int updateStock) throws Exception {
		
		try{	
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			
			String catalogsID = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getsCatalogId();
			//check sql
			int catalogStock = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getiNum();
			Logger.verifyEquals( updateStock,catalogStock, "SQL-Ymt_Catalogs iNum");
			
			//check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object> productStockInfoBean = new ProductStockInfoWapper().getByCatalogId(catalogsID);
				AssertService.assertResultEqual(MapUtil.hashMap("ProductId", productId, "CatalogId", catalogsID, "StockNum", updateStock),productStockInfoBean);
			}
			
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//check ProductDetailModel
				JSONObject productDetailModelBean = new JSONObject(new ProductDetailModelWapper().getProductId(productId).toString());
				int actStock = productDetailModelBean.getJSONArray("CatalogList").getJSONObject(0).getJSONArray("StockList").getJSONObject(0).getInt("Stock");
				Logger.verifyEquals(updateStock, actStock, "ProductDetailModel-Stock");
				
				int CatalogStock = Integer.parseInt(new ProductListingModelWapper().getProductId(productId).get("CatalogStock").toString());
				Logger.verifyEquals(CatalogStock, actStock, "ProductListModel-CatalogStock");
			
			}
			
		}catch(Exception e){
			
		}

	}
	
	
}
